package com.atelier.plugin.database.action;

import com.atelier.plugin.database.domain.DataSourceSettingConfigurable;
import com.atelier.plugin.database.domain.DatabaseConnectionInfo;
import com.atelier.plugin.database.domain.DatabaseType;
import com.atelier.plugin.database.ui.DataSourceSettingDialog;
import com.atelier.plugin.database.ui.DataSourceSettingEditor;
import com.atelier.plugin.database.util.DefaultNamingUtil;
import com.atelier.plugin.projectWizard.util.ICONS;
import com.atelier.plugin.service.DataBaseConnectionStorageService;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBList;
import net.sf.cglib.beans.BeanCopier;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.List;

/**
 * @author tangrufei
 * @date 2023-09-07 17:15
 */
public class MysqlConnection extends DumbAwareAction
{
    private static  DatabaseType DATABASETYPE=DatabaseType.MYSQL;
    private static  final String DEFAULTHOST="localhost";
    private static  final String ADD="MYSQL";
    private Boolean canShow=true;
    public MysqlConnection()
    {
        super(ADD,ADD, ICONS.getIcon(ADD));
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent event)
    {
        Project project = event.getProject();
        if (project == null) {
            return;
        }
        System.out.println("MysqlConnection  触发");
        DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo();
        databaseConnectionInfo.setName( DefaultNamingUtil.generateUniqueName());
        databaseConnectionInfo.setDatabaseType(DATABASETYPE);
        databaseConnectionInfo.setPort(DATABASETYPE.getDefaultPort().toString());
        databaseConnectionInfo.setHost(DEFAULTHOST);
        DataSourceSettingDialog instance = DataSourceSettingDialog.getInstance();
        DefaultListModel<DatabaseConnectionInfo> listModel =instance.getListModel();
        List<DatabaseConnectionInfo> connectionInfos =
                DataBaseConnectionStorageService.getInstance(project).getConnectionInfos();
        instance.getDatabaseLabel().setText("  DataBase");
        listModel.addElement(databaseConnectionInfo);
        DataSourceSettingDialog.getInstance().getDatasouceList().setSelectedValue(databaseConnectionInfo, true);
        if (canShow) {
            listModel.clear();
            for (DatabaseConnectionInfo connectionInfo : connectionInfos) {
                DatabaseConnectionInfo newConnectionInfo = new DatabaseConnectionInfo();
                BeanCopier beanCopier = BeanCopier.create(DatabaseConnectionInfo.class, DatabaseConnectionInfo.class, false);
                beanCopier.copy(connectionInfo, newConnectionInfo, null);
                listModel.addElement(newConnectionInfo);
            }
            databaseConnectionInfo.setName( DefaultNamingUtil.generateUniqueName());
            listModel.addElement(databaseConnectionInfo);
            DataSourceSettingDialog.getInstance().getDatasouceList().setSelectedValue(databaseConnectionInfo, true);
//            DataSourceSettingDialog.getInstance().getDatasouceList().requestFocusInWindow();
//            DataSourceSettingDialog.getInstance().getDatasouceList().requestFocus();
            DataSourceSettingConfigurable configurable = new DataSourceSettingConfigurable();
            DataSourceSettingEditor editor=new DataSourceSettingEditor(project,configurable);
            editor.setSize(800,600);
            editor.show();
        }


    }
    public MysqlConnection canShow(boolean b){
        this.canShow=b;
        return this;
    }

    @Override
    public void update(@NotNull AnActionEvent e)
    {
        super.update(e);
        // 使用Presentation对象设置图标左对齐
        Presentation presentation = e.getPresentation();
        presentation.setIcon(ICONS.getIcon(ADD));
         // 设置图标左对齐

        // 如果需要禁用或隐藏菜单项，可以在此处设置
        // presentation.setEnabled(false);
        // presentation.setVisible(false);
    }
}
